//
//  KindViewController.m
//  PersonalProject
//
//  Created by 卫建 on 15/12/17.
//  Copyright © 2015年 卫建. All rights reserved.
//

#import "KindViewController.h"
#import "Button.h"
#import "POP.h"
#import "UIView+XZMFrame.h"

//#import "CodeScanViewController.h"

#define XZMScreenW [UIScreen mainScreen].bounds.size.width
#define XZMScreenH [UIScreen mainScreen].bounds.size.height

@interface KindViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(nonatomic,weak)UIImageView *imageView;

@property(nonatomic,strong)UIButton *btn;

@property(nonatomic,strong)NSArray *images;
@property(nonatomic,strong)NSArray *titles;

//@property(nonatomic,strong)NSMutableArray *buttonArray;
@end


static NSInteger SpringFactor = 10;
static CGFloat SpringDelay = 0.1;

@implementation KindViewController







- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.userInteractionEnabled = NO;
    
    //添加返回button
    [self addUItabBarButton];
    
    // 添加响应button,这里的每个button对应的是每一个控制器
    // 数据
    self.images = @[@"toshop_lightup_button@2x", @"toshop_lightup_button@2x",@"toshop_lightup_button@2x", @"toshop_lightup_button@2x", @"toshop_lightup_button@2x", @"toshop_lightup_button@2x"];
    self.titles = @[@"跑步",@"走路",@"骑行",@"滑雪", @"滑冰", @"跑步机"];
    
    NSUInteger cols = 3;
    CGFloat btnW = 60;
    CGFloat btnH = btnW + 30;
    CGFloat beginMargin = 20;
    CGFloat middleMargin = (XZMScreenW - 2 * beginMargin - cols *btnW)/ (cols - 1);
    CGFloat btnStartY = (XZMScreenH - 2 * btnH) * 0.5;
    
    for (int i = 0; i < self.images.count; i++) {
        
        self.btn = [Button buttonWithType:UIButtonTypeCustom];
        
        NSInteger col = i % cols;
        NSInteger row = i / cols;
        
        CGFloat btnX = col * (middleMargin +btnW) + beginMargin;
        CGFloat btnY = row * btnH + btnStartY;
        
        [self.view addSubview:self.btn];
        
        [self.btn setTitle:self.titles[i] forState:UIControlStateNormal];
        
        [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.btn setImage:[UIImage imageNamed:self.images[i]] forState:UIControlStateNormal];
        
        [self.btn addTarget:self action:@selector(chickBtnDown:) forControlEvents:UIControlEventTouchDown];
        
        [self.btn addTarget:self action:@selector(chickBtnUpInside:clickButtonAtIndex:) forControlEvents:UIControlEventTouchUpInside];
        
        //        [self.btn addTarget:self action:@selector(chickBtnUpInside:) forControlEvents:UIControlEventTouchUpInside];
        
        self.btn.tag = i;//(很重要的tag值)
        
        CGFloat benginBtnY = btnStartY - XZMScreenH;
        
        /** 添加动画 */
        POPSpringAnimation *anima = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        
        anima.fromValue = [NSValue valueWithCGRect:CGRectMake(btnX, benginBtnY, btnW, btnH)];
        
        anima.toValue = [NSValue valueWithCGRect:CGRectMake(btnX, btnY, btnW, btnH)];
        
        anima.springSpeed = SpringFactor;
        
        anima.springBounciness = SpringDelay;
        
        anima.beginTime = CACurrentMediaTime() + i * SpringDelay;
        
        [self.btn pop_addAnimation:anima forKey:nil];
        
        [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
            
            
        }];
        
    }
    
    /** 添加sloganView指示条 */
    UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]];
    
    sloganView.y = -XZMScreenW;
    
    [self.view addSubview:sloganView];
    
    CGFloat centerX = XZMScreenW * 0.5;
    
    CGFloat centerEndY = XZMScreenH * 0.15;
    
    CGFloat centerBenginY = centerEndY - XZMScreenH;
    
    /** 添加动画 */
    POPSpringAnimation *anima = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerBenginY)];
    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];
    anima.springBounciness = SpringDelay;
    anima.beginTime = CACurrentMediaTime() + SpringDelay * self.images.count;
    
    anima.springSpeed = SpringFactor;
    
    [sloganView pop_addAnimation:anima forKey:nil];
    
    [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
        
        /** 动画完成后 */
        self.view.userInteractionEnabled = YES;
    }];
}

- (void)cancelWithCompletionBlock:(void (^)())block
{
    self.view.userInteractionEnabled = NO;
    
    
    int index = 0;
    for (int i = index; i < self.view.subviews.count; i++)
    {
        UIView *view = self.view.subviews[i];
        
        POPSpringAnimation *anima = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
        
        anima.springBounciness = SpringDelay;
        
        anima.springSpeed = SpringFactor;
        
        anima.beginTime = CACurrentMediaTime() + (i - index) * SpringDelay;
        
        CGFloat endCenterY = view.centerY + XZMScreenH;
        
        anima.toValue = [NSValue valueWithCGPoint:CGPointMake(view.centerX, endCenterY)];
        
        [view pop_addAnimation:anima forKey:nil];
        
        if (i == self.view.subviews.count - 1) { // 最后一个动画完成时
            
            [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
                
                [self dismissViewControllerAnimated:NO completion:nil];
                
                block();
            }];
            
        }
        
    }
    
}

- (void)chickBtnDown:(UIButton *)btn
{
    POPBasicAnimation *anima = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    anima.toValue = [NSValue valueWithCGSize:CGSizeMake(1.1, 1.1)];
    
    [btn pop_addAnimation:anima forKey:nil];
    
}


-(void)returnText:(ReturnTextBlock)block
{
    self.returnTextBlock = block;
}


#pragma mark-------kind界面上的button当前响应事件
- (void)chickBtnUpInside:(UIButton *)sender
      clickButtonAtIndex:(NSInteger)buttonIndex
{
    POPBasicAnimation *anima = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    
    anima.toValue = [NSValue valueWithCGSize:CGSizeMake(1.3, 1.3)];
    
    [sender pop_addAnimation:anima forKey:nil];
    
    //进行判断
    if (sender.tag == 0)
    {
        if (self.titles[0] != nil)
        {
            self.returnTextBlock(self.titles[0]);
        }
    
        [self animationButtonDownLoad];
    }
    else if (sender.tag == 1)
    {
        if (self.titles[1] != nil)
        {
            self.returnTextBlock(self.titles[1]);
        }
        
        [self animationButtonDownLoad];
    }
    else if (sender.tag == 2)
    {
        if (self.titles[2] != nil)
        {
            self.returnTextBlock(self.titles[2]);
        }
        
        [self animationButtonDownLoad];
    }
    else if (sender.tag == 3)
    {
        if (self.titles[3] != nil)
        {
            self.returnTextBlock(self.titles[3]);
        }
        
        [self animationButtonDownLoad];
    }
    else if (sender.tag == 4)
    {
        if (self.titles[4] != nil)
        {
            self.returnTextBlock(self.titles[4]);
        }
        
        [self animationButtonDownLoad];
    }
    else if (sender.tag == 5)
    {
        if (self.titles[5] != nil)
        {
            self.returnTextBlock(self.titles[5]);
        }
        
        [self animationButtonDownLoad];
    }
    
    /*
     buttonIndex = self.btn.tag;
     
     switch (buttonIndex)
     {
     //调用相册
     case 0:
     {
     UIImagePickerController *picker = [[UIImagePickerController alloc]init];
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     picker.delegate = self;
     picker.allowsEditing = YES;
     [self presentViewController:picker animated:YES completion:nil];
     
     break;
     }
     
     //调用二维码扫一扫
     case 1:
     {
     CodeScanViewController *codeScanVC = [[CodeScanViewController alloc]init];
     
     UINavigationController *codeScanNC = [[UINavigationController alloc]initWithRootViewController:codeScanVC];
     
     [self presentViewController:codeScanNC animated:YES completion:^{
     
     }];
     }
     
     //调用相机
     case 2:
     {
     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
     {
     UIImagePickerController *picker = [[UIImagePickerController alloc]init];
     picker.sourceType = UIImagePickerControllerSourceTypeCamera;
     picker.delegate = self;
     picker.allowsEditing = YES;
     [self presentViewController:picker animated:YES completion:nil];
     }
     else
     {
     NSLog(@"没有相机");
     }
     
     }
     
     default:
     break;
     }
     */
    
    anima.toValue = [NSValue valueWithCGSize:CGSizeMake(1.0, 1.0)];
}


#pragma mark---------点击每一个按钮滑落产生的动画效果
-(void)animationButtonDownLoad
{
    POPBasicAnimation *anima = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    
    anima.toValue = [NSValue valueWithCGSize:CGSizeMake(1.1, 1.1)];
    
    [self.btn pop_addAnimation:anima forKey:nil];
    
    anima.toValue = [NSValue valueWithCGSize:CGSizeMake(1.0, 1.0)];
    
    
    
    [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
        
        POPBasicAnimation *anima2 = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
        
        anima2.toValue = @(0);
        
        [self.btn pop_addAnimation:anima2 forKey:nil];
        
        [anima2 setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
            
            [self cancelWithCompletionBlock:^{
                // 切换对应控制器
                
                
            }];
        }];
        
    }];

}


//#pragma mark-------kind界面上的button对应的应该推出功能的响应事件
//-(void)didClickedWhichButtonAction
//{
//    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
//    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    picker.delegate = self;
//    picker.allowsEditing = YES;
//    [self presentViewController:picker animated:YES completion:nil];
//}


#pragma mark--------添加返回button
-(void)addUItabBarButton
{
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

#pragma mark--------添加返回button的响应事件
-(void)leftItemAction:(UIButton *)sender
{
    
    //    [sender addTarget:self action:@selector(chickBtnDown:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationController dismissViewControllerAnimated:NO completion:^{
        
    }];
}






@end
